<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="hxrjkxxsdTable"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="
        columns => {
          this.columns = columns;
        }
      "
      :isSelectColumn="false"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visible" :title="dialogTitle" destroyOnClose  :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <MonitorVehicleInfo :type="handleType" :rowData="formData" @close="closeHandler" />
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getVehicleMonitor, deleteVehicleMonitor, getOperator } from '@/api/customerRepair/monitor/monitorInfo';
import MonitorVehicleInfo from './MonitorVehicleInfo';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import dictCode from '@/utils/dictCode';

export default {
  name: 'monitorInfo',
  components: {
    MonitorVehicleInfo
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: getVehicleMonitor,
      fetchParams: { xhrAbort: true },
      handleType: '',
      formData: {},
      dialogTitle: '',
      visible: false,
      operatorList: []
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.searchOperator();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license',
          placeholder: '车牌号'
        },
        // {
        //   type: 'INPUT',
        //   label: '车主',
        //   fieldName: 'ownerName',
        //   placeholder: '车主'
        // },
        {
          type: 'SELECT',
          label: '操作员',
          fieldName: 'operator',
          filterable: true,
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: 'VIN'
        },
        {
          type: 'RANGE_DATE',
          label: '建档日期',
          style: { minWidth: '200px' },
          fieldName: 'foundStartDate|foundEndDate',
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择...',
          itemList: this.createDictList('1004'),
          initialValue: 10041001
        }
      ];
    },
    createTableColumns: function() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: (props, h) => {
            return <span> {props.row.index + 1} </span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button type="text" size="mini" onClick={() => this.editHandle(props.row)} disabled={this.disableButton(props.row)}>
                  编辑
                </el-button>
                <el-button type="text" size="mini" onClick={() => this.beforeDelete(props.row)} disabled={this.disableButton(props.row)}>
                  停用
                </el-button>
              </div>
            );
          }
        },
        {
          title: '接待提醒',
          dataIndex: 'repairOrderTag',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          render: (props, h) => {
            if (props.row.repairOrderTag == 10041001) return <i class="el-icon-check"></i>;
            else return <i class="el-icon-close"></i>;
          }
        },
        {
          title: '结算提醒',
          dataIndex: 'balanceTag',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          render: (props, h) => {
            if (props.row.balanceTag == 10041001) return <i class="el-icon-check"></i>;
            else return <i class="el-icon-close"></i>;
          }
        },
        {
          title: '预约提醒',
          dataIndex: 'bookingTag',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          render: (props, h) => {
            if (props.row.bookingTag == 10041001) return <i class="el-icon-check"></i>;
            else return <i class="el-icon-close"></i>;
          }
        },
        {
          title: '车牌号',
          dataIndex: 'license',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '发动机号',
        //   dataIndex: 'engineNo',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '车主编号',
        //   dataIndex: 'ownerNo',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '车主',
        //   dataIndex: 'ownerName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '提示信息',
          dataIndex: 'message',
          sorter: true,
          filter: true,
          filterType: 'input',
          // showOverflowTooltip: true,
          width: 300
        },
        {
          title: '开始日期',
          dataIndex: 'beginDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '结束日期',
          dataIndex: 'endDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '建档日期',
          dataIndex: 'foundDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        // {
        //   title: '销售开始日期',
        //   dataIndex: 'salesDateBegin',
        //   dateFormat: 'yyyy-MM-dd',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'date-range'
        // },
        // {
        //   title: '销售结束日期',
        //   dataIndex: 'salesDateEnd',
        //   dateFormat: 'yyyy-MM-dd',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'date-range'
        // },
        {
          title: '操作员',
          dataIndex: 'operator',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (props, h) => {
            return <span> {this.operatorTransfer(props.row.operator)} </span>;
          }
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          render: (props, h) => {
            return <span> {this.createDictText(props.row.isValid, '1004')} </span>;
          }
          // },
          // {
          //   title: '最低销售额',
          //   dataIndex: 'ownerConsumeAmountMin',
          //   sorter: true,
          //   filter: true,
          //   filterType: 'input'
          // },
          // {
          //   title: '最贵销售额',
          //   dataIndex: 'ownerConsumeAmountMax',
          //   sorter: true,
          //   filter: true,
          //   filterType: 'input'
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    beforeDelete(row) {
      this.$confirm('是否确定停用？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // console.log(row);
          this.deleteHandle(row);
        })
        .catch(() => {});
    },
    disableButton(row) {
      if (row.isValid == 10041002) return true;
      else return false;
    },
    async deleteHandle(row) {
      let res = {};
      // console.log(row.monitorId);
      res = await deleteVehicleMonitor(row.monitorId);
      this.fetchParams = { ...this.fetchParams };
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
      console.log(this.fetchParams);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      this.dialogTitle = `监控车主车辆${this.conf[this.handleType]}`;
      this.visible = true;
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    async searchOperator() {
      // this.operatorList = (await getOperator({})).data.map(x => ({ text: x.employeeName, value: x.employeeNo }));
      let res = await getOperator();
      console.log(res, 'res');
      this.operatorList = res.data.map(x => ({ text: x.employeeName, value: x.userId }));
      // this.topFilterList[2].itemList = this.operatorList;
      this.topFilterList.find(item => item.fieldName === 'operator').itemList = this.operatorList;
      console.log(this.operatorList, '员工列表');
    },
    operatorTransfer(val) {
      console.log('val', val);
      let operatorName = val;
      this.operatorList.forEach(x => {
        if (x.value + '' == val) {
          operatorName = x.text;
        }
      });
      return operatorName;
      // return val;
    }
  }
};
</script>

<style scoped></style>
